package com.example.skyline.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.skyline.entity.Menu;
import com.example.skyline.entity.UserRole;
import com.example.skyline.entity.UserPermission;
import com.example.skyline.mapper.MenuMapper;
import com.example.skyline.mapper.UserRoleMapper;
import com.example.skyline.mapper.UserPerMissionMapper;
import com.example.skyline.service.MenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>  implements MenuService {

    @Resource
    private UserRoleMapper userRoleMapper;

    @Resource
    private UserPerMissionMapper userPerMissionMapper;

    @Resource
    private MenuMapper menuMapper;

    // 根据用户信息 ---> 获取用户菜单列表
    @Override
    public List<Menu> fetchMenusForRole(Integer userId) { // 可以去构造where条件
        // 根据用户ID查询角色ID
        LambdaQueryWrapper<UserRole> roleQueryWrapper = new LambdaQueryWrapper<>();
        roleQueryWrapper.eq(UserRole::getUserId, userId);
        UserRole role = userRoleMapper.selectOne(roleQueryWrapper);

        // 查询角色ID ---> 拥有的菜单ID
        LambdaQueryWrapper<UserPermission> menuQueryWrapper = new LambdaQueryWrapper<>();
        menuQueryWrapper.eq(UserPermission::getRoleId, role.getRoleId());
        List<UserPermission> userPermissionList = userPerMissionMapper.selectList(menuQueryWrapper);

        // 查询角色ID ---> 拥有的菜单ID ---> 查询菜单列表
        // 查询菜单列表 ---> 菜单列表排序 ---> 1.父级菜单排序 ---> 2.子菜单排序
        List<Integer> menuIds = userPermissionList.stream()
                .map(UserPermission::getMenuId)
                .collect(Collectors.toList());
        if (menuIds == null || menuIds.isEmpty()) {
            return Collections.emptyList(); // 如果menuIds为空，返回空列表
        }

        LambdaQueryWrapper<Menu> menusParentQueryWrapper = new LambdaQueryWrapper<>();
        menusParentQueryWrapper.in(Menu::getMenuId, menuIds);
        List<Menu> menusParent = menuMapper.selectList(menusParentQueryWrapper);

        // 拼接父子菜单列表
        List<Menu> menus = new ArrayList<>();
        for (Menu menuParent : menusParent) {
            LambdaQueryWrapper<Menu> menusChildQueryWrapper = new LambdaQueryWrapper<>();
            menusChildQueryWrapper.eq(Menu::getParentId, menuParent.getMenuId());
            List<Menu> menusChild =  menuMapper.selectList(menusChildQueryWrapper);
            menuParent.setChildren(menusChild); // 添加子菜单列表
            menus.add(menuParent);
        }

        return menus;
    }

    // 根据父级菜单ID ---> 获取对应的子菜列表
    @Override
    public List<Menu> fetchAllMenus(Integer parentId) {
        LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        menuLambdaQueryWrapper.eq(Menu::getParentId, parentId);
        List<Menu> menus = menuMapper.selectList(menuLambdaQueryWrapper);
        return menus;
    }

    // 获取所有父级菜单列表 ---> parentId = 0 ---> 获取所有一级菜单
    @Override
    public List<Menu> fetchAllParentMenus() {
        LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        menuLambdaQueryWrapper.eq(Menu::getParentId, 0);
        List<Menu> menus = menuMapper.selectList(menuLambdaQueryWrapper);
        return menus;
    }

}